﻿using System;

namespace UniRx.Operators
{
	// Token: 0x0200031D RID: 797
	internal class TakeWhileObservable<T> : OperatorObservableBase<T>
	{
		// Token: 0x060010A7 RID: 4263 RVA: 0x0004CAE0 File Offset: 0x0004AEE0
		public TakeWhileObservable(IObservable<T> source, Func<T, bool> predicate) : base(source.IsRequiredSubscribeOnCurrentThread<T>())
		{
			this.source = source;
			this.predicate = predicate;
		}

		// Token: 0x060010A8 RID: 4264 RVA: 0x0004CAFC File Offset: 0x0004AEFC
		public TakeWhileObservable(IObservable<T> source, Func<T, int, bool> predicateWithIndex) : base(source.IsRequiredSubscribeOnCurrentThread<T>())
		{
			this.source = source;
			this.predicateWithIndex = predicateWithIndex;
		}

		// Token: 0x060010A9 RID: 4265 RVA: 0x0004CB18 File Offset: 0x0004AF18
		protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
		{
			if (this.predicate != null)
			{
				return new TakeWhileObservable<T>.TakeWhile(this, observer, cancel).Run();
			}
			return new TakeWhileObservable<T>.TakeWhile_(this, observer, cancel).Run();
		}

		// Token: 0x040009C7 RID: 2503
		private readonly IObservable<T> source;

		// Token: 0x040009C8 RID: 2504
		private readonly Func<T, bool> predicate;

		// Token: 0x040009C9 RID: 2505
		private readonly Func<T, int, bool> predicateWithIndex;

		// Token: 0x0200031E RID: 798
		private class TakeWhile : OperatorObserverBase<T, T>
		{
			// Token: 0x060010AA RID: 4266 RVA: 0x0004CB40 File Offset: 0x0004AF40
			public TakeWhile(TakeWhileObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
			}

			// Token: 0x060010AB RID: 4267 RVA: 0x0004CB51 File Offset: 0x0004AF51
			public IDisposable Run()
			{
				return this.parent.source.Subscribe(this);
			}

			// Token: 0x060010AC RID: 4268 RVA: 0x0004CB64 File Offset: 0x0004AF64
			public override void OnNext(T value)
			{
				bool flag;
				try
				{
					flag = this.parent.predicate(value);
				}
				catch (Exception error)
				{
					try
					{
						this.observer.OnError(error);
					}
					finally
					{
						base.Dispose();
					}
					return;
				}
				if (flag)
				{
					this.observer.OnNext(value);
				}
				else
				{
					try
					{
						this.observer.OnCompleted();
					}
					finally
					{
						base.Dispose();
					}
				}
			}

			// Token: 0x060010AD RID: 4269 RVA: 0x0004CC04 File Offset: 0x0004B004
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x060010AE RID: 4270 RVA: 0x0004CC3C File Offset: 0x0004B03C
			public override void OnCompleted()
			{
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x040009CA RID: 2506
			private readonly TakeWhileObservable<T> parent;
		}

		// Token: 0x0200031F RID: 799
		private class TakeWhile_ : OperatorObserverBase<T, T>
		{
			// Token: 0x060010AF RID: 4271 RVA: 0x0004CC74 File Offset: 0x0004B074
			public TakeWhile_(TakeWhileObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
			}

			// Token: 0x060010B0 RID: 4272 RVA: 0x0004CC85 File Offset: 0x0004B085
			public IDisposable Run()
			{
				return this.parent.source.Subscribe(this);
			}

			// Token: 0x060010B1 RID: 4273 RVA: 0x0004CC98 File Offset: 0x0004B098
			public override void OnNext(T value)
			{
				bool flag;
				try
				{
					flag = this.parent.predicateWithIndex(value, this.index++);
				}
				catch (Exception error)
				{
					try
					{
						this.observer.OnError(error);
					}
					finally
					{
						base.Dispose();
					}
					return;
				}
				if (flag)
				{
					this.observer.OnNext(value);
				}
				else
				{
					try
					{
						this.observer.OnCompleted();
					}
					finally
					{
						base.Dispose();
					}
				}
			}

			// Token: 0x060010B2 RID: 4274 RVA: 0x0004CD48 File Offset: 0x0004B148
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x060010B3 RID: 4275 RVA: 0x0004CD80 File Offset: 0x0004B180
			public override void OnCompleted()
			{
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x040009CB RID: 2507
			private readonly TakeWhileObservable<T> parent;

			// Token: 0x040009CC RID: 2508
			private int index;
		}
	}
}
